package com.dongdongmovie.service.impl;

import com.dongdongmovie.mapper.TabCategoryMapper;
import com.dongdongmovie.pojo.TabCategory;
import com.dongdongmovie.pojo.TabCategoryExample;
import com.dongdongmovie.service.TabCategory_Service;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@DubboService
public class TabCategory_ServiceImpl implements TabCategory_Service {

    //注入mapper
    @Autowired
    private TabCategoryMapper tabCategoryMapper;

    //查询全部
    @Override
    public List<TabCategory> show() {
        return tabCategoryMapper.selectByExample(null);
    }

    //添加数据
    @Override
    public int add(TabCategory t) {
        return tabCategoryMapper.insertSelective(t);
    }

    //修改数据
    @Override
    public int update(TabCategory t) {
        return tabCategoryMapper.updateByPrimaryKeySelective(t);
    }

    //根据id进行查询
    @Override
    public TabCategory showbyId(Integer cid) {
        return tabCategoryMapper.selectByPrimaryKey(cid);
    }

    //根据id删除数据
    @Override
    public int deletebyId(Integer cid) {
        return tabCategoryMapper.deleteByPrimaryKey(cid);
    }

    //批量删除数据
    @Override
    public int deleteall(Integer[] cids) {
        List<Integer> list = Arrays.asList(cids);
        TabCategoryExample te = new TabCategoryExample();
        TabCategoryExample.Criteria criteria = te.createCriteria();
        criteria.andCidIn(list);
        return tabCategoryMapper.deleteByExample(te);
    }

    //向上移动
    @Override
    public int updategoup(Integer cid) {
        List<TabCategory> list = tabCategoryMapper.selectByExample(null);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getCid() == cid) {
                if (i == 0) {
                    return 0;
                }
                TabCategory t = new TabCategory();
                TabCategory t1 = new TabCategory();
                t.setCid(list.get(i).getCid());
                t.setCname(list.get(i - 1).getCname());
                t1.setCid(list.get(i - 1).getCid());
                t1.setCname(list.get(i).getCname());
                tabCategoryMapper.updateByPrimaryKey(t);
                tabCategoryMapper.updateByPrimaryKey(t1);
            }
        }
        return 1;
    }

    //向下移动
    @Override
    public int updatedecline(Integer cid) {
        List<TabCategory> list = tabCategoryMapper.selectByExample(null);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getCid() == cid) {
                if (i == list.size() - 1) {
                    return 0;
                }
                TabCategory t = new TabCategory();
                TabCategory t1 = new TabCategory();
                t.setCid(list.get(i).getCid());
                t.setCname(list.get(i + 1).getCname());
                t1.setCid(list.get(i + 1).getCid());
                t1.setCname(list.get(i).getCname());
                tabCategoryMapper.updateByPrimaryKey(t);
                tabCategoryMapper.updateByPrimaryKey(t1);
            }
        }
        return 1;
    }
}
